//
//  CFile.h
//

#include "CFile.h"
#include <stdio.h>

#pragma region CFile getters

std::wstring CFile::getPath(void) const
{
	return this->wsPath;
}

std::wstring CFile::getName(void) const
{
	return this->wsName;
}

std::wstring CFile::getSize(void) const
{
	return this->wsSize;
}

//SYSTEMTIME CFile::getDataOfCreation(void) const
//{
//	return this->stDataOfCreation;
//}

#pragma endregion


CFile::CFile(const wchar_t* wcPath)
{
	this->wsPath = wcPath;
	this->wsName = PathFindFileName(wcPath);
	this->wsSize = this->getFileSize(this->wsPath);
	//this->setDataOfCreation();
}

CFile::~CFile(void)
{

}


std::wstring CFile::getFileSize(const std::wstring &fileName)
{
	std::wstring wsFileSize(L"| ");
	std::ifstream file(fileName.c_str(), std::ifstream::in | std::ifstream::binary);

	if(!file.is_open())
	{
		wsFileSize.append(L"0");
	}
	else
	{
		file.seekg(0, std::ios::end);
		wsFileSize.append(std::to_wstring(file.tellg()));
	}
	file.close();

	wsFileSize.append(L" bytes.");
	return wsFileSize;
}


bool CFile::compareFiles(const CFile &f1, const CFile &f2)
{
	return (f1.getName().compare(f2.getName()) < 0 ) ? true : false;
}


//SYSTEMTIME CFile::setDataOfCreation()
//{
//	// Get handle to out file in read mode
//	// For more infornation :
//	// http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx
//	HANDLE hFile = CreateFile(this->getPath().c_str(),
//		GENERIC_READ, FILE_SHARE_READ, NULL,
//		OPEN_EXISTING, 0, NULL);
//
//	if(hFile != INVALID_HANDLE_VALUE)
//	{
//		FILETIME ftCreate, ftAccess, ftWrite;
//		SYSTEMTIME stUTC, stLocal;
//
//		// Get data of creation
//		// For more information :
//		// http://msdn.microsoft.com/en-us/library/ms724320%28v=vs.85%29.aspx
//		if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
//		{
//
//			// Convert the last-write time to local time.
//			FileTimeToSystemTime(&ftWrite, &stUTC);
//			SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &this->stDataOfCreation);
//		}
//	}
//
//	CloseHandle(hFile);
//	return this->stDataOfCreation;
//}

std::wstring CFile::to_wstr() const
{
	std::wstring str;

	str.append(
		this->getName() + L" | " + this->getSize() +
		/*L" | " + std::to_wstring(this->getDataOfCreation().wYear) +
		L":" + std::to_wstring(this->getDataOfCreation().wMonth) +
		L":" + std::to_wstring(this->getDataOfCreation().wMonth) +*/ L"\n"
		);

	return str;
}